package com.example.musicplayer

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MusicList:Fragment() {

    private val musicData=ArrayList<music>()  //存放music list数据的集合

    private lateinit var mediaPlayer:MediaPlayer //延迟初始化一个全局音乐播放器

    private lateinit var musicList:RecyclerView  //延迟初始化滚动列表

    private lateinit var musicBottomImage:ImageView  //延迟初始化底部图片

    private lateinit var musicBottomTitle:TextView  //延迟初始化底部标题

    private lateinit var musicBottomSinger:TextView  //延迟初始化底部歌手名

    private lateinit var changeImage:ImageButton  //延迟初始化切换按钮

    private lateinit var stopImage:ImageButton  //延迟初始化停止按钮


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view=inflater.inflate(R.layout.musiclist,container,false)
        musicList=view.findViewById(R.id.musicList)  ////拿到MusicList这个Fragment实例中xml布局的滚动列表对象
        //剩下的不用我说了吧
        musicBottomImage=view.findViewById(R.id.musicBottomImage)
        musicBottomTitle=view.findViewById(R.id.musicBottomTitle)
        musicBottomSinger=view.findViewById(R.id.musicBottomSinger)
        changeImage=view.findViewById(R.id.changeImage)
        stopImage=view.findViewById(R.id.stopImage)
        mediaPlayer=MediaPlayer()
        return view
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //制作音乐列表,网络请求包括解析数据获取数据
        val JSONWithMusicListService=ServiceCreator.create(JSONImageService::class.java)
        JSONWithMusicListService.getListData().enqueue(object : Callback<List<music>> {
            override fun onResponse(call: Call<List<music>>, response: Response<List<music>>) {
                val list=response.body()
                if (list!=null) {
                    for (i in list) {
                        musicData.add(i)
                    }
                }
                val layoutManager=LinearLayoutManager(context)
                musicList.layoutManager=layoutManager
                val adapter=MusicListAdapter(musicData) { music -> playMusic(music) }
                musicList.adapter=adapter
            }
            override fun onFailure(p0: Call<List<music>>, p1: Throwable) {
                TODO("Not yet implemented")
            }
        })
    }
    //设置底部音乐播放器
    private fun playMusic(music: music?) {
        if (music==null) {
            return
        }
        else {
            Glide.with(this@MusicList)
                .load(music.coverImgUrl)
                .into(musicBottomImage)
            musicBottomTitle.text=music.title
            musicBottomSinger.text=music.singer
            changeImage.setImageResource(R.drawable.play)
            stopImage.setImageResource(R.drawable.replay)
            initMediaPlayer(music.src)
            setupChangeImageClickListener()
            setupStopImageClickListener(music.src)
        }
    }
    //音乐播放器资源释放和初始化
    private fun initMediaPlayer(musicUrl:String) {
        //释放MediaPlayer资源
        mediaPlayer.release()
        //重新初始化
        mediaPlayer=MediaPlayer.create(requireContext(), Uri.parse(musicUrl))
        //返回最初按钮状态
        changeImage.setImageResource(R.drawable.play)
    }
    //播放or暂停按钮
    private fun setupChangeImageClickListener() {
        changeImage.setOnClickListener {
            if (!mediaPlayer.isPlaying) {
                mediaPlayer.start()
                changeImage.setImageResource(R.drawable.pause)
            }
            else {
                mediaPlayer.pause()
                changeImage.setImageResource(R.drawable.play)
            }
        }
    }
    //停止按钮
    private fun setupStopImageClickListener(musicUrl:String) {
        stopImage.setOnClickListener {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
            else if (!mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
        }
    }
}